package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.EtiologyBase;
import com.itheima.service.EtiologyService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/uploadetiology")
public class EtiologyController {
    @Reference
    private EtiologyService etiologyService;



    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);/*读取了上传的文件*/
            List<EtiologyBase> data = new ArrayList<>();
            for (String[] string : strings) {
                EtiologyBase etiologyBase=new EtiologyBase();
                etiologyBase.setLoreID(string[0]);
                etiologyBase.setTitle(string[1]);
                etiologyBase.setKeyword(string[2]);
                etiologyBase.setLoreType(string[3]);
                etiologyBase.setLoreContent(string[4]);
                etiologyBase.setIsDelete(string[5]);
                etiologyBase.setRemark(string[6]);
                etiologyBase.setChronicDisease(string[7]);
                data.add(etiologyBase);
            }
            etiologyService.add(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }
    @RequestMapping("/findPage1")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = etiologyService.pageQuery(queryPageBean);
        return pageResult;
    }


}
